package com.heng.application.mocklocation

import android.content.Context
import android.os.Bundle
import android.widget.Button
import android.widget.CheckBox
import android.widget.EditText
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
import com.heng.application.R

/**
 * @Author    : zhangheng227
 * date       : 2025/5/12
 */
class MockLocationActivity: AppCompatActivity() {

    private var tv_longitude: EditText? = null
    private var tv_latitude: EditText? = null
    private var tv_doaction: Button? = null
    private var cb_switch: CheckBox? = null

    companion object{
        private const val PREFERENCES_NAME = "location_prefs"
        private const val LATITUDE_KEY = "latitude"
        private const val LONGITUDE_KEY = "longitude"
        private const val MOCK_SWITCH_KEY = "mock_switch"
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_mock_location)

        initView()
        restoreData()
        initListeners()
    }

    private fun restoreData() {
        // 使用LocationPreferences工具类获取保存的位置信息
        val location = com.heng.application.utils.LocationPreferences.getSavedLocation(this)
        tv_longitude?.setText(location?.longitude?.toString() ?: "0.0")
        tv_latitude?.setText(location?.latitude?.toString() ?: "0.0")
        
        // 获取模拟位置开关状态
        val isChecked = com.heng.application.utils.LocationPreferences.isMockEnabled(this)
        cb_switch?.isChecked = isChecked
        
        if (isChecked) {
            tv_doaction?.setBackgroundColor(ContextCompat.getColor(this, R.color.enable_switch))
        } else {
            tv_doaction?.setBackgroundColor(ContextCompat.getColor(this, R.color.unable_switch))
        }
    }

    private fun initView(){
        tv_longitude = findViewById(R.id.tv_mock_longitude)
        tv_latitude = findViewById(R.id.tv_mock_latitude)
        tv_doaction = findViewById(R.id.tv_doaction)
        cb_switch = findViewById(R.id.cb_switch)
    }

    private fun initListeners() {
        tv_doaction?.setOnClickListener {
            val latitudeStr = tv_latitude?.text.toString()
            val longitudeStr = tv_longitude?.text.toString()
            
            try {
                val latitude = latitudeStr.toDouble()
                val longitude = longitudeStr.toDouble()
                
                if (latitude < -90 || latitude > 90) {
                    tv_latitude?.error = "纬度范围必须在-90到90之间"
                    return@setOnClickListener
                }
                
                if (longitude < -180 || longitude > 180) {
                    tv_longitude?.error = "经度范围必须在-180到180之间"
                    return@setOnClickListener
                }
                
                val isChecked = cb_switch?.isChecked ?: false
                
                // 使用LocationPreferences工具类保存位置信息
                com.heng.application.utils.LocationPreferences.saveLocation(
                    this,
                    latitude,
                    longitude,
                    isChecked
                )
                
                if (isChecked) {
                    tv_doaction?.setBackgroundColor(ContextCompat.getColor(this, R.color.enable_switch))
                    Toast.makeText(this, "已开启模拟位置,杀掉app重启生效", Toast.LENGTH_SHORT).show()
                } else {
                    tv_doaction?.setBackgroundColor(ContextCompat.getColor(this, R.color.unable_switch))
                    Toast.makeText(this, "已关闭模拟位置,请杀掉app重启生效", Toast.LENGTH_SHORT).show()
                }
            } catch (e: NumberFormatException) {
                if (!latitudeStr.matches(Regex("^-?\\d*\\.?\\d*$"))) {
                    tv_latitude?.error = "请输入有效的数字"
                }
                if (!longitudeStr.matches(Regex("^-?\\d*\\.?\\d*$"))) {
                    tv_longitude?.error = "请输入有效的数字"
                }
            }
        }
    }
}